package com.itcam.phaselist.list_;

import java.util.Iterator;
import java.util.LinkedList;

public class LinkedListCRUD {
    public static void main(String[] args) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);
        System.out.println("linkedList = " + linkedList);

        // 删除节点
        linkedList.remove();// 默认删除第一个节点
        System.out.println("linkedList = " + linkedList);

        // 修改某个结点对象
        linkedList.set(1, 999);
        System.out.println("linkedList = " + linkedList);

        // 得到某个结点对象
        int i = linkedList.get(1);
        System.out.println("i = " + i);

        // LinkedList是实现了List接口,遍历方式可以是迭代器
        System.out.println("\n===LinkeList 遍历迭代器====");
        Iterator<Integer> iterator = linkedList.iterator();
        while (iterator.hasNext()) {
            Integer next = iterator.next();
            System.out.print("next = " + next + '\t');
        }

        System.out.println("\n===LinkeList 遍历增强for====");
        for (Integer n : linkedList) {
            System.out.print("n = " + n + '\t');
        }

        System.out.println("\n===LinkeList 遍历普通for====");
        for (int j = 0; j < linkedList.size(); j++) {
            System.out.print("j = " + linkedList.get(j) + '\t');
        }
    }
}
